package Model;

import java.util.ArrayList;
import java.util.Date;

/**
 * Complex task as required by assignment 3
 * @author psyomn
 */
public class ComplexTask extends Task
{

	/**
	 * Construct!
	 * 
	 * @param iTaskName
	 * @param iDueDate
	 * @param iType
	 */
	public ComplexTask(String iTaskName, Date iDueDate, String iType)
	{
		super(iTaskName, iDueDate, iType);
	}

	public ComplexTask(ComplexTask complex)
	{
		super(complex.getName(), complex.getDueDate(), complex.getType());
	}

	/**
	 * Add a task to the list
	 * 
	 * @param task
	 */
	public void addTodoItem(Task task)
	{
		mTodo.add(task);
		
		super.datainterface.addSubtask(task.getName(),
		    task.getDueDate(), task.getType());
	}

	/**
	 * Sets the status of an individual subtask
	 * 
	 * @param wIndex
	 * @param wStatus
	 */
	public void setTodoItemStatus(int wIndex, boolean wStatus)
	{
		mTodo.get(wIndex).setDone(wStatus);
	}

	/**
	 * This function overrides the parent function as it has different
	 * functionality (this one has to print each subtask)
	 * 
	 * @return the stringified versions of the tasks
	 */
	@Override
	public String toString()
	{
		String str = new String();
		str += "[";
		str += super.getName();
		str += "|";
		str += super.getDueDate();
		str += "|";
		str += super.getType();
		str += "]";

		/* Now we want to get stringified task for each task */

		for (Task t : mTodo)
		{
			str += "\n\t";
			str += t.toString();
		}

		str += "\n";

		return str;
	}

	/**
	 * Set all the tasks to whatever is needed if the root task is set as so...
	 * 
	 * @param iDone
	 */
	@Override
	public void setDone(boolean iDone)
	{
		super.setDone(iDone); // N.B. do not change to this.setDone -- will cause
													// stack overflow!!
        datainterface.setDone(iDone);
        state = olddirtystate;
        
		for (Task t : mTodo)
		{
			t.setDone(iDone);
			t.getDatainterface().setDone(iDone);
			t.setState("olddirtystate");
		}
	}

	/**
	 * Remove task depending on memory reference
	 * 
	 * @param t
	 */
	public void removeTask(Task t)
	{
		for (int i = 0; i < mTodo.size(); ++i)
		{
			if (mTodo.get(i).equals(t))
			{
				mTodo.get(i).setState("olddeletestate");
				mTodo.get(i).remove();
				mTodo.remove(i);
				i = mTodo.size();
			}
		}
	}

  /**
	 * To get the task list for whatever reason
	 * 
	 * @return a list of task items. Can be complex, single, periodic.
	 */
	public ArrayList<Task> getTodo()
	{
		return mTodo;
	}

	/**
	 * Returns the list of subtasks
	 */
	@Override
	public ArrayList<Task> getSubtasks()
	{
		return mTodo;
	}

	/**
	 * Returns the due date for single tasks, Interval for periodic tasks or
	 * "Complex" for complex tasks
	 * 
	 * @return
	 */
	public String getDateString()
	{
		return "Complex";
	}
}
